//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System.ComponentModel;
using System.Workflow.ComponentModel;
using Microsoft.Research.ScientificWorkflow.Helper;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    /// <summary>
    /// The OpenDAP URI generator activity is the seed activity for 
    /// generating an OpenDAP URI for fetching data from the open DAP
    /// server. 
    /// This is a stand alone activity which means that the URI generated
    /// by this activity is a valid OpenDAP server query. However the URI 
    /// generated does not support filtering. To use that this activity 
    /// should be followed by either of Projections and Selections activities.
    /// The URI genearated by this activity is in the format
    /// http://<<servername>>/<<FileName>> for e.g.
    /// http://plasmus.ocean.washington.edu:8080/opendap/ROMS/HC_bio_seasonal_nowind_his_0800.nc
    /// </summary>
    [Name("OpenDap URI Query Generator", "OpenDap Query")]
    [Description("Generates a URI which can be used to fetch data from an Open DAP server.")]
    [WorkflowCategory("OpenDap")]
    class OpenDAPURIGenerator : Activity
    {
        #region Property Definitions

        public static DependencyProperty ServerNameProperty = DependencyProperty.Register("ServerName",
            typeof(string), typeof(OpenDAPURIGenerator));

        /// <summary>
        /// Name of the Open DAP Server.
        /// </summary>
        [RequiredInputParam]
        [Name("Open DAP Server Name")]
        [Description("Name of the Open DAP server to be queried.")]
        public string ServerName
        {
            get { return ((string)(base.GetValue(OpenDAPURIGenerator.ServerNameProperty))); }
            set { base.SetValue(OpenDAPURIGenerator.ServerNameProperty, value); }
        }

        public static DependencyProperty DAPFileProperty = DependencyProperty.Register("DAPFile",
            typeof(string), typeof(OpenDAPURIGenerator));

        /// <summary>
        /// Open DAP file for reading the data.
        /// </summary>
        [RequiredInputParam]
        [Name("Open DAP File Name")]
        [Description("Name of the Open DAP file to be read.")]
        public string DAPFile
        {
            get { return ((string)(base.GetValue(OpenDAPURIGenerator.DAPFileProperty))); }
            set { base.SetValue(OpenDAPURIGenerator.DAPFileProperty, value); }
        }

        public static DependencyProperty DAPUriProperty = DependencyProperty.Register("DAPUri",
            typeof(OpenDAPURI), typeof(OpenDAPURIGenerator));

        /// <summary>
        /// Open DAP Uri generated after appending the server name
        /// and the file name.
        /// </summary>
        [OutputParam]
        [Name("Open DAP Uri")]
        [Description("Open DAP Uri generated after merging the parameters.")]
        public OpenDAPURI DAPUri
        {
            get { return ((OpenDAPURI)(base.GetValue(OpenDAPURIGenerator.DAPUriProperty))); }
            set { base.SetValue(OpenDAPURIGenerator.DAPUriProperty, value); }
        }

        #endregion

        #region Overridden Methods

        /// <summary>
        /// Appends the parameters to create a open dap URI and
        /// sets the dap uri output property
        /// </summary>
        /// <param name="executionContext"></param>
        /// <returns></returns>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            OpenDAPURI uri = new OpenDAPURI();
            
            uri.ServerName = this.ServerName;
            uri.OpenDAPFile = this.DAPFile;

            this.DAPUri = uri;

            return ActivityExecutionStatus.Closed;
        }

        #endregion
    }
}
